package com.econ.powercloud.ems.controller;

import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.econ.powercloud.common.base.facade.pojo.PageResult;
import com.econ.powercloud.ems.aop.UserOperatePower;
import com.econ.powercloud.ems.pojo.*;
import com.econ.powercloud.ems.pojo.requestVO.*;
import com.econ.powercloud.ems.service.biz.EmsAdvancedStrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author : zjf
 * @Description : 高级策略controller
 * @Date : 2022/10/20 11:16
 * @Modified by : zjf
 */
@RestController
@Validated
@Api(tags ="高级策略")
@RequestMapping(value = "/api/v2/advanced/strategy")
public class EmsAdvancedStrategyController extends ApiBaseController{

    @Autowired
    private EmsAdvancedStrategyService emsAdvancedStrategyService;

    @ApiOperation(value = "新增/修改配置点号")
    @PostMapping(value = "saveConfigDit/{module}")
    public MsgResult saveConfigDit(@PathVariable Integer module,
                                   @RequestBody @Valid List<ModuleConfigRequestVO> requestVOList){
        emsAdvancedStrategyService.saveConfigDit(module,requestVOList);
        return successHandler("设置成功!");
    }

    @ApiOperation(value = "新增/修改常量设定值")
    @PostMapping(value = "saveConstantSet/{module}")
    public MsgResult<String> saveConstantSet(@PathVariable Integer module,
                                     @RequestBody @Valid List<ConstantSetRequestVO> requestVOList){
        emsAdvancedStrategyService.saveConstantSet(module,requestVOList);
        return successHandler("设置成功!");
    }

    @ApiOperation(value = "修改某个常量设定值")
    @PostMapping(value = "modifyConstantSet/{id}")
    public MsgResult<String> modifyConstantSet(@PathVariable Integer id,
                                             @RequestBody @Valid ConstantSetEdit edit){
        emsAdvancedStrategyService.modifyConstantSet(id,edit);
        return successHandler("设置成功!");
    }

    @ApiOperation(value = "获取配置点号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "module", value = "模块位置", dataType = "Integer")
    })
    @GetMapping(value = "getConfigDit/{module}")
    public MsgResult<List<ModuleConfigVO>> getConfigDit(@PathVariable Integer module){
        return successHandler(emsAdvancedStrategyService.getConfigDit(module));
    }

    @ApiOperation(value = "获取常量配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "module", value = "模块位置", dataType = "Integer")
    })
    @GetMapping(value = "getConfigConstant/{module}")
    public MsgResult<List<ConstantConfigVO>> getConfigConstant(@PathVariable Integer module){
        return successHandler(emsAdvancedStrategyService.getConfigConstant(module));
    }

    @ApiOperation(value = "根据模块位置获取实时数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "module", value = "模块位置", dataType = "Integer")
    })
    @GetMapping(value = "getFlashData/{module}")
    public MsgResult<List<FlashVO>> getFlashDataByModule(@PathVariable Integer module){
        return successHandler(emsAdvancedStrategyService.getFlashDataByModule(module));
    }

    @ApiOperation(value = "根据模块位置获取历史数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "module", value = "模块位置", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "isAsc", value = "是否升序", dataType = "boolean"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间",dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间",dataType = "long"),
    })
    @GetMapping(value = "getHisDataByModule")
    public MsgResult<List<HisChartDataVO>> getHisDataByModule(@RequestParam("module") Integer module,
                                                              @RequestParam("isAsc") Boolean isAsc,
                                                              @RequestParam(value = "startTime") Long startTime,
                                                              @RequestParam(value = "endTime") Long endTime){
        return successHandler(emsAdvancedStrategyService.getHisDataByModule(module, isAsc, startTime, endTime));
    }

    @ApiOperation(value = "获取历史指令数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "module", value = "模块位置", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "isAsc", value = "是否升序", dataType = "boolean"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间",dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间",dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "页",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "分页个数",dataType = "Integer"),
    })
    @GetMapping(value = "getHisOrderDataByModule")
    public MsgResult<PageResult<List<ChartDataVO>>> getHisOrderDataByModule(@RequestParam("module") Integer module,
                                                                            @RequestParam("isAsc") Boolean isAsc,
                                                                            @RequestParam(value = "startTime") Long startTime,
                                                                            @RequestParam(value = "endTime") Long endTime,
                                                                            @RequestParam(value = "pageNo")Integer pageNo,
                                                                            @RequestParam(value = "pageSize")Integer pageSize){
        return successHandler(emsAdvancedStrategyService.getHisDataByModule(module, isAsc, startTime, endTime,pageNo,pageSize));
    }

    @ApiOperation(value = "获取所有策略")
    @GetMapping(value = "getAllStrategy")
    public MsgResult<List<StrategyVO>> getAllStrategy(){
        return successHandler(emsAdvancedStrategyService.getAllStrategy());
    }

    @ApiOperation(value = "高级策略配置")
    @PostMapping(value = "strategyConfig")
    public MsgResult<String> strategy(@RequestBody @Valid SingleListRequestVO<StrategyConfigRequestVO> strategyConfigRequestVOList){
        emsAdvancedStrategyService.strategyConfig(strategyConfigRequestVOList.getList());
        return successHandler("配置成功!");
    }

    @ApiOperation(value = "策略控制配置点号")
    @PostMapping(value = "/saveStrategyControl")
    public MsgResult<String> saveStrategyControl(@RequestBody @Valid SingleListRequestVO<StrategyControlRequestVO> strategyControlRequestVOList){
        emsAdvancedStrategyService.saveStrategyControl(strategyControlRequestVOList.getList());
        return successHandler("配置成功!");
    }

    @ApiOperation(value = "获取策略控制配置点号")
    @GetMapping(value = "/getStrategyControl")
    public MsgResult<List<StrategyControlVO>> getStrategyControl(){
        return successHandler(emsAdvancedStrategyService.getStrategyControl());
    }

    @ApiOperation(value = "根据策略id获取对应策略控制数据")
    @GetMapping(value = "/control/{strategyId}")
    public MsgResult<StrategyControlVO> control(@PathVariable String strategyId,
                                                @RequestParam(required = false) Integer groupId){
        return successHandler(emsAdvancedStrategyService.control(strategyId,groupId));
    }

    /**
     * 投入
     * @return
     */
    @UserOperatePower
    @ApiOperation(value = "投入/重试")
    @PostMapping("active")
    public MsgResult<Object> active(@RequestBody @Validated AdvancedStrategyActiveOrQuitRequestVO requestVO) {
        emsAdvancedStrategyService.activeOrQuit(requestVO.getModule(),requestVO.getGroupId(),1);
        return this.successHandler(true);
    }

    /**
     * 退出
     * @return
     */
    @UserOperatePower
    @ApiOperation(value = "退出")
    @PostMapping("quit")
    public MsgResult<Object> quit(@RequestBody @Validated AdvancedStrategyActiveOrQuitRequestVO requestVO) {
        emsAdvancedStrategyService.activeOrQuit(requestVO.getModule(),requestVO.getGroupId(),0);
        return this.successHandler(true);
    }

}
